<?php

namespace app\api\controller\v1;

use app\api\validate\Count;
use app\api\validate\IDMustBePositiveInt;
use app\api\model\Product as ProductModel;
use app\lib\exception\ProductException;

class Product 
{
    public function getRecent($count=15){
        (new Count)->goCheck();
        $products = ProductModel::getMostRecent($count);
        if($products->isEmpty()){
            throw new ProductException();
        };
        $products = $products->hidden(['summary']);
        return $products;
    }


    public function getAllInCategory($id){
        (new IDMustBePositiveInt)->goCheck();
        $products = ProductModel::getProdustByCategory($id);
        if(empty($products)){
            throw new ProductException();
        };
        $products->hidden(['sumarry']);
        return $products;
    }


    public function getOne($id){
        (new IDMustBePositiveInt)->goCheck();
        $product = ProductModel::getProductDetail($id);
        if(empty($product)){
            throw new ProductException(); 
        };
        return $product;
    }

}
